<?php

namespace Query\Logic;

use common\Table\TdSelectIdfaFromFddIosTable;
use common\Factory\Factory;

class QueryLogic {
    
    private function getIdfaQueryString($idfa) {
        $newIdfaItems = [];
        //每批200条idfa
        $feng = 200;
        $count = substr_count($idfa, ',') + 1;
        //共几批
        $whileLen = ceil($count / $feng);
        
        if($whileLen > 1) {
            for($i = 0; $i < $whileLen; $i++) {
                if(strpos($idfa, ',') === false) {
                    $newString = "'" . filter_var($idfa, FILTER_SANITIZE_STRING) . "'";
                    $newIdfaItems[] = str_replace(',', "','", $newString);
                    break;
                }
                //将idfa字符串中','号按照一批最大条数替换成'|'号
                $idfaReplaceString = preg_replace('/\,/', '|', $idfa, $feng);
                //查找最后一个 '|'号下标
                $lastLableIndex = strrpos($idfaReplaceString, '|');
                //按照此下标截取字符串就获得每一批idfa数据
                $newString = "'" . filter_var(substr($idfa, 0, $lastLableIndex), FILTER_SANITIZE_STRING) . "'";
                $newIdfaItems[] = str_replace(',', "','", $newString);
                //把已经获得的idfa数据从原始idfa字符串中剔除
                $idfa = substr($idfa, $lastLableIndex + 1);
            }
        }
        else {
             $newString = "'" . filter_var($idfa, FILTER_SANITIZE_STRING) . "'";
             $newIdfaItems[] = str_replace(',', "','", $newString);
        }
                
        return $newIdfaItems;
    }
    
    public function queryIdfaToRedis($data, $redis) {
        Factory::getServer('MqWriteLog')->send('query idfa to redis raw input params:' . json_encode($data));
        $idfa = $data['idfa'];
        $newIdfaItems = [];
        $result = new \stdClass();
        if(strpos($idfa, ',') > 0) {
            $idfaItems = explode(',', $idfa);
            $count = count($idfaItems);
            if($count > 0) {
                $newIdfaItems = $idfaItems;
            }
        }
        else if(!empty($idfa)) {
            $newIdfaItems[] = $idfa;
        }
        
        if(count($newIdfaItems) > 0) {
            foreach($newIdfaItems as $item) {
                $sourceItem = $item;
                $item = crc32($item);
                $item = sprintf("%u", $item);
                $isExists = $redis->sismember('idfaList', $item);
                $isExists = $isExists === true ? 1 : 0;
                $result->$sourceItem = $isExists;
            }
            
            Factory::getServer('MqWriteLog')->send('query idfa to redis output params:' . json_encode($result));
        }
        else {
            Factory::getServer('MqWriteLog')->send('query idfa to redis error:idfa empty');
        }
        
        return $result;
    }
    
    public function queryIdfa($data) {
        Factory::getServer('MqWriteLog')->send('query idfa raw input params:' . json_encode($data));
        if (empty($data['idfa'])) {
            Factory::getServer('MqWriteLog')->send('query idfa error:idfa empty');
            return false;
        }

        $idfaSource = $data['idfa'];
        $tdSelectIdfaFromFddIosTable = new TdSelectIdfaFromFddIosTable();
        $idfaSelectStringArray = $this->getIdfaQueryString($idfaSource);
        $items = new \stdClass();
        foreach($idfaSelectStringArray as $arrayKey => $selectSelect) {
            $result = $tdSelectIdfaFromFddIosTable->selectIdfa($selectSelect, $data['appid']);
            if(result === false) continue;
            foreach($result as $key => $idfa) {
                count($idfaSelectStringArray) == 1 && $arrayKey = $key;
                $temp = $idfa['idfa'];
                $items->$temp = 1;
                //idfa源字符串中剔除已经查到的idfa
                $idfaSource = preg_replace('/'. $idfa['idfa'] .'[\,]*/', '', $idfaSource);
            }
        }
        $lastReturn = $items;
        
        $idfaSource = explode(',', rtrim($idfaSource, ','));
        if(count($idfaSource) > 0) {
            $item = new \stdClass();
            foreach($idfaSource as $idfa) {
                if(!empty($idfa)) $items->$idfa = 0;
            }
            $lastReturn = $items;
        }

        Factory::getServer('MqWriteLog')->send('query idfa output params:' . json_encode($lastReturn));
        return $lastReturn;
    }
}